/**
 *
 */
package jMiiEditor.gui.checker;

import jMiiEditor.mii.checker.MiiChecker;
import jMiiEditor.mii.checker.MiiCheckerDefault;
import jMiiEditor.mii.checker.MiiCheckerResult;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

//VS 4E -- DO NOT REMOVE THIS LINE!
public class JMiiEditorCheckJDialog extends JDialog
{
    private static final long serialVersionUID = 1L;
    private JButton jButton_Cancel;
    private JButton jButton_DoIt;
    private JButton jButton_OK;
    private JButton jButton_Purge;
    private JLabel jLabel_FileName;
    private JPanel jPanel_Bottom;
    private JPanel jPanel_Top;
    private JScrollPane jScrollPane_CheckResult;
    private JTable jTable_CheckResult;
    private JTextField jTextField_FileName;
    private MiiCheckerResultTableModel miiCheckerResultTableModel;
    private MiiCheckerTableModel miiCheckerTableModel;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    private JMiiEditorCheckJDialog()
    {
        initComponents();

        java.io.InputStream is = getClass().getResourceAsStream("/jMiiEditor/gui/default.mii");
        jMiiEditor.mii.Mii mii;

        if( is != null ) {
            try {
                mii = new jMiiEditor.mii.Mii( is );
            }
            catch( java.io.IOException e ) {
                throw new RuntimeException( e );
            }
        }
        else {
            throw new RuntimeException( "Can't create Mii" );
        }

        MiiChecker checker = new MiiChecker(mii);

        checker.performAllChecks(
            new MiiCheckerDefault()
            {
                @Override
                public byte[] getSystemId()
                {
                    return null;
                }
                @Override
                public String getFileName()
                {
                    return "foo";
                }
                
            }
            );

        initFixComponents();
        setTableModel(checker);
    }

    public JMiiEditorCheckJDialog( Frame parent )
    {
        super(parent);

        initComponents();
        initFixComponents();

        super.setDefaultCloseOperation( JMiiEditorCheckJDialog.DISPOSE_ON_CLOSE );
        super.getContentPane().setPreferredSize( this.getSize() );
        super.pack();
        super.setLocationRelativeTo( parent );
    }

    private void initFixComponents()
    {
    }
    
//    public void initI18n(I18nInterface anI18n )
//    {
//        // Apply i18n !
//        AutoI18n autoI18n = new AutoI18n( this, anI18n );
//        autoI18n.performeI18N();
//    }

    public void setTableModel(MiiChecker checker)
    {
        super.setTitle( "Check Mii" );
        jButton_OK.setVisible( false );

        miiCheckerTableModel = new MiiCheckerTableModel( checker );
        miiCheckerTableModel.addTableModelListener(miiCheckerTableModel);
        jTable_CheckResult = miiCheckerTableModel.getJTable();
        jTable_CheckResult.setModel( miiCheckerTableModel );
        jScrollPane_CheckResult.setViewportView(jTable_CheckResult);
        miiCheckerTableModel.setColumnWidth( jTable_CheckResult );
    }

    public void setTableModel(MiiCheckerResult miiCheckerResult)
    {
        super.setTitle( "Check Mii Result" );
        jButton_Cancel.setVisible( false );
        jButton_Purge.setVisible( false );
        jButton_DoIt.setVisible( false );

        miiCheckerResultTableModel = new MiiCheckerResultTableModel( miiCheckerResult );
        miiCheckerResultTableModel.addTableModelListener(miiCheckerTableModel);
        jTable_CheckResult.setModel( miiCheckerResultTableModel );
        miiCheckerResultTableModel.setColumnWidth( jTable_CheckResult );
    }

    public void setFileName( String filename )
    {
        if( filename != null ) {
            jTextField_FileName.setText( filename );
        }
    }

    private void initComponents() {
    	setTitle("Check Mii");
    	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	setMinimumSize(new Dimension(640, 250));
    	setPreferredSize(new Dimension(640, 250));
    	setFont(new Font("Dialog", Font.PLAIN, 12));
    	setMaximumSize(new Dimension(640, 250));
    	setBackground(new Color(212, 208, 200));
    	setForeground(Color.black);
    	add(getJPanel_Top(), BorderLayout.NORTH);
    	add(getJScrollPane_CheckResult(), BorderLayout.CENTER);
    	add(getJPanel_Bottom(), BorderLayout.SOUTH);
    	addComponentListener(new ComponentAdapter() {
    
    		public void componentResized(ComponentEvent event) {
    			componentComponentResized(event);
    		}
    	});
    	addWindowListener(new WindowAdapter() {
    
    		public void windowClosing(WindowEvent event) {
    			windowWindowClosing(event);
    		}
    	});
    	setSize(632, 223);
    }

    private JButton getJButton_OK() {
        if (jButton_OK == null) {
            jButton_OK = new JButton();
            jButton_OK.setText("OK");
            jButton_OK.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_OKMouseMousePressed(event);
                }
            });
        }
        return jButton_OK;
    }

    private JPanel getJPanel_Top() {
        if (jPanel_Top == null) {
            jPanel_Top = new JPanel();
            jPanel_Top.setLayout(new BoxLayout(jPanel_Top, BoxLayout.X_AXIS));
            jPanel_Top.add(getJLabel_FileName());
            jPanel_Top.add(getJTextField_FileName());
        }
        return jPanel_Top;
    }

private JTextField getJTextField_FileName() {
        if (jTextField_FileName == null) {
            jTextField_FileName = new JTextField();
            jTextField_FileName.setText("@");
            jTextField_FileName.setColumns( 60 );
            jTextField_FileName.setEditable( false );
        }
        return jTextField_FileName;
    }

    private JLabel getJLabel_FileName() {
        if (jLabel_FileName == null) {
            jLabel_FileName = new JLabel();
            jLabel_FileName.setText("Filename:");
        }
        return jLabel_FileName;
    }

    private JPanel getJPanel_Bottom() {
        if (jPanel_Bottom == null) {
            jPanel_Bottom = new JPanel();
            jPanel_Bottom.add(getJButton_Cancel());
            jPanel_Bottom.add(getJButton_Purge());
            jPanel_Bottom.add(getJButton_OK());
            jPanel_Bottom.add(getJButton_DoIt());
        }
        return jPanel_Bottom;
    }

    private JButton getJButton_Cancel() {
        if (jButton_Cancel == null) {
            jButton_Cancel = new JButton();
            jButton_Cancel.setText("Cancel");
            jButton_Cancel.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_CancelMouseMousePressed(event);
                }
            });
        }
        return jButton_Cancel;
    }

    private JButton getJButton_DoIt() {
        if (jButton_DoIt == null) {
            jButton_DoIt = new JButton();
            jButton_DoIt.setText("Do it");
            jButton_DoIt.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_DoItMouseMousePressed(event);
                }
            });
        }
        return jButton_DoIt;
    }
    private JButton getJButton_Purge() {
        if (jButton_Purge == null) {
            jButton_Purge = new JButton();
            jButton_Purge.setText("Purge");
            jButton_Purge.addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent event) {
                    jButton_PurgeMouseMousePressed(event);
                }
            });
        }
        return jButton_Purge;
    }

    private JScrollPane getJScrollPane_CheckResult() {
        if (jScrollPane_CheckResult == null) {
            jScrollPane_CheckResult = new JScrollPane();
            jScrollPane_CheckResult.setViewportView(getJTable_CheckResult());
        }
        return jScrollPane_CheckResult;
    }

    private JTable getJTable_CheckResult() {
        if (jTable_CheckResult == null) {
            jTable_CheckResult = new JTable();
        }
        return jTable_CheckResult;
    }

    @SuppressWarnings("unused")
    private static void installLnF() {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if (lnfClassname == null)
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
            System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage());
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     * @param args
     */
    public static void main( String[] args )
    {
        installLnF();
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run()
            {
                JMiiEditorCheckJDialog dialog = new JMiiEditorCheckJDialog();
                dialog.setDefaultCloseOperation( JMiiEditorCheckJDialog.DISPOSE_ON_CLOSE );
                dialog.setTitle( "Check Mii (debug)" );
                dialog.getContentPane().setPreferredSize( dialog.getSize() );
                dialog.pack();
                dialog.setLocationRelativeTo( null );
                dialog.setVisible( true );
            }
        } );
    }

    private void componentComponentResized(ComponentEvent event) {
        System.out.println( "Resize: " + event.getComponent().getSize() );
    }

    private void windowWindowClosing(WindowEvent event) 
    {
        cancelOperations();
    }
    
    private void jButton_CancelMouseMousePressed(MouseEvent event)
    {
        cancelOperations();
        dispose();
    }
    
    private void cancelOperations()
    {
        // Do nothing
        miiCheckerTableModel.clearMiiCheckerResultList();
    }

    private void jButton_PurgeMouseMousePressed(MouseEvent event)
    {
        // apply filter
        miiCheckerTableModel.purgeMiiCheckerResultList();
        miiCheckerTableModel.fireTableDataChanged();
    }

    private void jButton_DoItMouseMousePressed(MouseEvent event)
    {
        System.out.println( "jButton_DoItMouseMousePressed: " + event.getComponent() );

        // apply filter
        jButton_PurgeMouseMousePressed(event);

        dispose();
    }

    private void jButton_OKMouseMousePressed(MouseEvent event)
    {
        dispose();
    }

}
